// pages/answerChallengeTest/answerChallengeTest.js
const {
    getBarAndTitleHeight,
    jump,
    stringify,
    goBack
} = require("../../utils/utils.js");
import request from '../../utils/request'
const api = require("../../config/api");

Page({

    /**
     * 页面的初始数据
     */
    data: {
        typeList: ['单选', '多选', '判断', '填空'],
        // 状态栏&标题栏高度
        ...getBarAndTitleHeight(true),
        QIndex: 1,
        question: [] //答题数据
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.setData({
            ...getBarAndTitleHeight(),
        });
        this.getQuestion(false)
    },
    //获取答题数据
    async getQuestion(isNext = false) {
        const res = await request.get(api.questionRandomList, {
            number: 50
        }, {
            loading: false
        })
        console.log('QuestionList', res, res?.data?.data?.map(item => item.answer));
        if (res.httpStatus === 200 && res?.data?.data) {
            // 如果是正在答题 继续请求数据
            if (isNext) {
                res.data.data.forEach(item => {
                    this.data.question.push(item)
                })
                this.data.QIndex++
                this.setData({
                    question: this.data.question,
                    QIndex: this.data.QIndex
                })
                this.loading = false
            } else {
                this.setData({
                    question: res.data.data
                })
                // 断网 提交答题
                wx.onNetworkStatusChange(res => {
                    if (!res.isConnected) {
                        goBack()
                    }
                })
            }
        }
    },

    /**
     * 选择答案
     * @param {*} e 
     */
    submit(e) {
        if (this.loading) return
        this.loading = true
        let {
            QIndex,
            question
        } = this.data
        console.log(question, QIndex)
        if (!question[QIndex - 1]['option' + e.currentTarget.dataset.value]) {
            this.loading = false
            return
        }
        question[QIndex - 1]['usersAnswer'] = e.currentTarget.dataset.value
        // 判断当前选择是否正确
        if (e.currentTarget.dataset.value === question[QIndex - 1]['answer']) {
            question[QIndex - 1]['isRight'] = true
        } else {
            question[QIndex - 1]['isRight'] = false
        }
        this.setData({
            question
        })
        setTimeout(() => {
            // 如果答对进入下一题
            if (question[QIndex - 1]['isRight']) {
                // 如果到最后一题  继续请求数据
                if (QIndex === this.data.question.length) {
                    this.getQuestion(true)
                } else {
                    QIndex++
                    this.setData({
                        QIndex
                    })
                    this.loading = false
                }
            } else { //答错直接提交
                console.log('question', question);
                this.setData({
                    isSubmit: true
                })
                this.submitMyAnswer(question)
                this.setData({
                    QIndex
                })
            }
        }, 500)
    },
    //提交答案
    async submitMyAnswer(question) {
        let arr = []
        question.map((item) => {
            let obj = {
                ...item
            }
            arr.push(obj)
        })
        console.log('params', arr);
        const result = await request.post(api.continuousRightSubmitAnswer, {
            data: arr
        },{ loading: false})
        console.log('result', result);
        if (result.httpStatus === 200) {
            if(this.data.isSubmit){
                wx.redirectTo({
                    url: `/pages/answerChallengeResult/answerChallengeResult?data=${stringify(result.data)}`,
                })
            }
        }else{
            this.setData({
                isSubmit: false
            })
        }
        this.loading = false
    },

    //放弃答题
    giveUpAnswer() {
        if (this.loading) return
        this.modal.show()
    },
    //弹窗取消
    modalCancel() {
        this.modal.hide()
    },
    // 弹窗确定
    modalConfirm() {
        if(this.loading) return 
        this.loading = true
        this.setData({
            isSubmit: true
        })
        this.submitMyAnswer(this.data.question)
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
        this.modal = this.selectComponent('#modal')
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        // 页面退出提交（非提交后跳转）
        if (!this.data.isSubmit) {
            this.submitMyAnswer(this.data.question)
        }
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },
})